<!DOCTYPE html>
<html lang=zh>
<head>
  <meta charset="utf-8">
  
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no, minimal-ui">
  <meta name="renderer" content="webkit">
  <meta http-equiv="Cache-Control" content="no-transform" />
  <meta http-equiv="Cache-Control" content="no-siteapp" />
  <meta name="apple-mobile-web-app-capable" content="yes">
  <meta name="apple-mobile-web-app-status-bar-style" content="black">
  <meta name="format-detection" content="telephone=no,email=no,adress=no">
  <!-- Color theme for statusbar -->
  <meta name="theme-color" content="#000000" />
  <!-- 强制页面在当前窗口以独立页面显示,防止别人在框架里调用页面 -->
  <meta http-equiv="window-target" content="_top" />
  
  
  <title>Java类型转换 | 张占阳的博客</title>
  <meta name="description" content="Java一共有八种基本数据类型  整数类型 1字节:byte 表数范围为-128(-27)~127(27-1)   2字节:short -32768(-215)~32767 (215-1)(大约为32万)   4字节:int -2147483648(-231)~2147483647(231-1)(大约为21亿)   8字节:long -263~263-1     字符类型 2字节:char   浮">
<meta property="og:type" content="article">
<meta property="og:title" content="Java类型转换">
<meta property="og:url" content="https://zzy521.com/posts/16b8fe02.html">
<meta property="og:site_name" content="张占阳的博客">
<meta property="og:description" content="Java一共有八种基本数据类型  整数类型 1字节:byte 表数范围为-128(-27)~127(27-1)   2字节:short -32768(-215)~32767 (215-1)(大约为32万)   4字节:int -2147483648(-231)~2147483647(231-1)(大约为21亿)   8字节:long -263~263-1     字符类型 2字节:char   浮">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2021-03-20T07:27:14.000Z">
<meta property="article:modified_time" content="2021-03-23T07:40:54.956Z">
<meta property="article:author" content="张占阳">
<meta property="article:tag" content="Java">
<meta property="article:tag" content="类型转换">
<meta name="twitter:card" content="summary">
  <!-- Canonical links -->
  <link rel="canonical" href="https://zzy521.com/posts/16b8fe02.html">
  
    <link rel="alternate" href="/atom.xml" title="张占阳的博客" type="application/atom+xml">
  
  
    <link rel="icon" href="/favicon.png" type="image/x-icon">
  
  
<link rel="stylesheet" href="/css/style.css">

  
  
  
  
<meta name="generator" content="Hexo 5.3.0"></head>


<body class="main-center" itemscope itemtype="http://schema.org/WebPage">
  <header class="header" itemscope itemtype="http://schema.org/WPHeader">
  <div class="slimContent">
    <div class="navbar-header">
      
      
      <div class="profile-block text-center">
        <a id="avatar" href="https://github.com/zzy2019520" target="_blank">
          <img class="img-circle img-rotate" src="/images/avatar.jpg" width="200" height="200">
        </a>
        <h2 id="name" class="hidden-xs hidden-sm">张占阳</h2>
        <h3 id="title" class="hidden-xs hidden-sm hidden-md">软件开发工程师</h3>
        <small id="location" class="text-muted hidden-xs hidden-sm"><i class="icon icon-map-marker"></i> 徐州, 中国</small>
      </div>
      
      <div class="search" id="search-form-wrap">

    <form class="search-form sidebar-form">
        <div class="input-group">
            <input type="text" class="search-form-input form-control" placeholder="搜索" />
            <span class="input-group-btn">
                <button type="submit" class="search-form-submit btn btn-flat" onclick="return false;"><i class="icon icon-search"></i></button>
            </span>
        </div>
    </form>
    <div class="ins-search">
  <div class="ins-search-mask"></div>
  <div class="ins-search-container">
    <div class="ins-input-wrapper">
      <input type="text" class="ins-search-input" placeholder="想要查找什么..." x-webkit-speech />
      <button type="button" class="close ins-close ins-selectable" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
    </div>
    <div class="ins-section-wrapper">
      <div class="ins-section-container"></div>
    </div>
  </div>
</div>


</div>
      <button class="navbar-toggle collapsed" type="button" data-toggle="collapse" data-target="#main-navbar" aria-controls="main-navbar" aria-expanded="false">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
    </div>
    <nav id="main-navbar" class="collapse navbar-collapse" itemscope itemtype="http://schema.org/SiteNavigationElement" role="navigation">
      <ul class="nav navbar-nav main-nav menu-highlight">
        
        
        <li class="menu-item menu-item-home">
          <a href="/.">
            
            <i class="icon icon-home-fill"></i>
            
            <span class="menu-title">首页</span>
          </a>
        </li>
        
        
        <li class="menu-item menu-item-archives">
          <a href="/archives">
            
            <i class="icon icon-archives-fill"></i>
            
            <span class="menu-title">归档</span>
          </a>
        </li>
        
        
        <li class="menu-item menu-item-categories">
          <a href="/categories">
            
            <i class="icon icon-folder"></i>
            
            <span class="menu-title">分类</span>
          </a>
        </li>
        
        
        <li class="menu-item menu-item-tags">
          <a href="/tags">
            
            <i class="icon icon-tags"></i>
            
            <span class="menu-title">标签</span>
          </a>
        </li>
        
        
        <li class="menu-item menu-item-about">
          <a href="/about">
            
            <i class="icon icon-cup-fill"></i>
            
            <span class="menu-title">关于</span>
          </a>
        </li>
        
      </ul>
      
	
    <ul class="social-links">
    	
        <li><a href="https://github.com/zzy2019520" target="_blank" title="Github" data-toggle=tooltip data-placement=top><i class="icon icon-github"></i></a></li>
        
        <li><a href="https://weibo.com/u/6000361757" target="_blank" title="Weibo" data-toggle=tooltip data-placement=top><i class="icon icon-weibo"></i></a></li>
        
    </ul>

    </nav>
  </div>
</header>

  
    <aside class="sidebar" itemscope itemtype="http://schema.org/WPSideBar">
  <div class="slimContent">
    
      <div class="widget">
    <h3 class="widget-title">公告</h3>
    <div class="widget-body">
        <div id="board">
            <div class="content">
                <p>欢迎交流与分享经验!</p>
            </div>
        </div>
    </div>
</div>

    
      
  <div class="widget">
    <h3 class="widget-title">分类</h3>
    <div class="widget-body">
      <ul class="category-list"><li class="category-list-item"><a class="category-list-link" href="/categories/Java/">Java</a><span class="category-list-count">3</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/Python/">Python</a><span class="category-list-count">1</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/">开发工具</a><span class="category-list-count">6</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/%E6%96%87%E7%AB%A0%E5%88%86%E7%B1%BB/">文章分类</a><span class="category-list-count">1</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/%E6%9C%8D%E5%8A%A1%E5%99%A8/">服务器</a><span class="category-list-count">4</span></li></ul>
    </div>
  </div>


    
      
  <div class="widget">
    <h3 class="widget-title">标签</h3>
    <div class="widget-body">
      <ul class="tag-list" itemprop="keywords"><li class="tag-list-item"><a class="tag-list-link" href="/tags/EOS/" rel="tag">EOS</a><span class="tag-list-count">3</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Hexo/" rel="tag">Hexo</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Java/" rel="tag">Java</a><span class="tag-list-count">4</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Linux/" rel="tag">Linux</a><span class="tag-list-count">3</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Rest%E6%8E%A5%E5%8F%A3/" rel="tag">Rest接口</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/SQLServer/" rel="tag">SQLServer</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Tomcat/" rel="tag">Tomcat</a><span class="tag-list-count">2</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/a/" rel="tag">a++</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/excel/" rel="tag">excel</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/exe/" rel="tag">exe</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/java/" rel="tag">java</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/markdown/" rel="tag">markdown</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/python/" rel="tag">python</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E4%BD%8D%E8%BF%90%E7%AE%97/" rel="tag">位运算</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E6%95%B0%E6%8D%AE%E5%BA%93/" rel="tag">数据库</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E6%96%87%E7%AB%A0%E6%A0%87%E7%AD%BE/" rel="tag">文章标签</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E7%B1%BB%E5%9E%8B%E8%BD%AC%E6%8D%A2/" rel="tag">类型转换</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E9%92%89%E9%92%89%E6%B6%88%E6%81%AF/" rel="tag">钉钉消息</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E9%A1%B9%E7%9B%AE%E8%AF%B4%E6%98%8E/" rel="tag">项目说明</a><span class="tag-list-count">1</span></li></ul>
    </div>
  </div>


    
      
  <div class="widget">
    <h3 class="widget-title">标签云</h3>
    <div class="widget-body tagcloud">
      <a href="/tags/EOS/" style="font-size: 13.67px;">EOS</a> <a href="/tags/Hexo/" style="font-size: 13px;">Hexo</a> <a href="/tags/Java/" style="font-size: 14px;">Java</a> <a href="/tags/Linux/" style="font-size: 13.67px;">Linux</a> <a href="/tags/Rest%E6%8E%A5%E5%8F%A3/" style="font-size: 13px;">Rest接口</a> <a href="/tags/SQLServer/" style="font-size: 13px;">SQLServer</a> <a href="/tags/Tomcat/" style="font-size: 13.33px;">Tomcat</a> <a href="/tags/a/" style="font-size: 13px;">a++</a> <a href="/tags/excel/" style="font-size: 13px;">excel</a> <a href="/tags/exe/" style="font-size: 13px;">exe</a> <a href="/tags/java/" style="font-size: 13px;">java</a> <a href="/tags/markdown/" style="font-size: 13px;">markdown</a> <a href="/tags/python/" style="font-size: 13px;">python</a> <a href="/tags/%E4%BD%8D%E8%BF%90%E7%AE%97/" style="font-size: 13px;">位运算</a> <a href="/tags/%E6%95%B0%E6%8D%AE%E5%BA%93/" style="font-size: 13px;">数据库</a> <a href="/tags/%E6%96%87%E7%AB%A0%E6%A0%87%E7%AD%BE/" style="font-size: 13px;">文章标签</a> <a href="/tags/%E7%B1%BB%E5%9E%8B%E8%BD%AC%E6%8D%A2/" style="font-size: 13px;">类型转换</a> <a href="/tags/%E9%92%89%E9%92%89%E6%B6%88%E6%81%AF/" style="font-size: 13px;">钉钉消息</a> <a href="/tags/%E9%A1%B9%E7%9B%AE%E8%AF%B4%E6%98%8E/" style="font-size: 13px;">项目说明</a>
    </div>
  </div>

    
      
  <div class="widget">
    <h3 class="widget-title">归档</h3>
    <div class="widget-body">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/archives/2021/05/">五月 2021</a><span class="archive-list-count">1</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2021/04/">四月 2021</a><span class="archive-list-count">1</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2021/03/">三月 2021</a><span class="archive-list-count">4</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2021/02/">二月 2021</a><span class="archive-list-count">3</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2021/01/">一月 2021</a><span class="archive-list-count">3</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2020/12/">十二月 2020</a><span class="archive-list-count">3</span></li></ul>
    </div>
  </div>


    
      
  <div class="widget">
    <h3 class="widget-title">最新文章</h3>
    <div class="widget-body">
      <ul class="recent-post-list list-unstyled no-thumbnail">
        
          <li>
            
            <div class="item-inner">
              <p class="item-category">
                <a class="category-link" href="/categories/%E6%96%87%E7%AB%A0%E5%88%86%E7%B1%BB/">文章分类</a>
              </p>
              <p class="item-title">
                <a href="/posts/ab81ff07.html" class="title">CRM调用外部接口</a>
              </p>
              <p class="item-date">
                <time datetime="2021-05-18T01:48:01.000Z" itemprop="datePublished">2021-05-18</time>
              </p>
            </div>
          </li>
          
          <li>
            
            <div class="item-inner">
              <p class="item-category">
                <a class="category-link" href="/categories/%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/">开发工具</a>
              </p>
              <p class="item-title">
                <a href="/posts/6a850357.html" class="title">调用钉钉接口发送工作通知</a>
              </p>
              <p class="item-date">
                <time datetime="2021-04-07T03:21:19.000Z" itemprop="datePublished">2021-04-07</time>
              </p>
            </div>
          </li>
          
          <li>
            
            <div class="item-inner">
              <p class="item-category">
                <a class="category-link" href="/categories/%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/">开发工具</a>
              </p>
              <p class="item-title">
                <a href="/posts/21754c85.html" class="title">解决NavicatPremium连接SQLServer出现未发现数据源名称并且未指定默认驱动</a>
              </p>
              <p class="item-date">
                <time datetime="2021-03-30T02:34:02.000Z" itemprop="datePublished">2021-03-30</time>
              </p>
            </div>
          </li>
          
          <li>
            
            <div class="item-inner">
              <p class="item-category">
                <a class="category-link" href="/categories/Java/">Java</a>
              </p>
              <p class="item-title">
                <a href="/posts/dad1e78f.html" class="title">Java中a=a++的问题</a>
              </p>
              <p class="item-date">
                <time datetime="2021-03-23T09:41:17.000Z" itemprop="datePublished">2021-03-23</time>
              </p>
            </div>
          </li>
          
          <li>
            
            <div class="item-inner">
              <p class="item-category">
                <a class="category-link" href="/categories/Java/">Java</a>
              </p>
              <p class="item-title">
                <a href="/posts/f789481e.html" class="title">Java位运算</a>
              </p>
              <p class="item-date">
                <time datetime="2021-03-23T02:05:47.000Z" itemprop="datePublished">2021-03-23</time>
              </p>
            </div>
          </li>
          
      </ul>
    </div>
  </div>
  

    
  </div>
</aside>

  
  
<aside class="sidebar sidebar-toc collapse" id="collapseToc" itemscope itemtype="http://schema.org/WPSideBar">
  <div class="slimContent">
    <nav id="toc" class="article-toc">
      <h3 class="toc-title">文章目录</h3>
      <ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E8%87%AA%E5%8A%A8%E7%B1%BB%E5%9E%8B%E8%BD%AC%E6%8D%A2"><span class="toc-number">1.</span> <span class="toc-text">自动类型转换</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%BC%BA%E5%88%B6%E7%B1%BB%E5%9E%8B%E8%BD%AC%E6%8D%A2"><span class="toc-number">2.</span> <span class="toc-text">强制类型转换</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%AD%97%E7%AC%A6%E4%B8%B2%E7%B1%BB%E5%9E%8B%E4%B8%8E%E5%85%B6%E4%BB%96%E7%B1%BB%E5%9E%8B%E7%9A%84%E8%BD%AC%E6%8D%A2"><span class="toc-number">3.</span> <span class="toc-text">字符串类型与其他类型的转换</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%85%B6%E5%AE%83%E7%B1%BB%E5%9E%8B%E5%90%91%E5%AD%97%E7%AC%A6%E4%B8%B2%E7%9A%84%E8%BD%AC%E6%8D%A2"><span class="toc-number">3.0.1.</span> <span class="toc-text">其它类型向字符串的转换</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%AD%97%E7%AC%A6%E4%B8%B2%E4%BD%9C%E4%B8%BA%E5%80%BC-%E5%90%91%E5%85%B6%E5%AE%83%E7%B1%BB%E5%9E%8B%E7%9A%84%E8%BD%AC%E6%8D%A2"><span class="toc-number">3.0.2.</span> <span class="toc-text">字符串作为值,向其它类型的转换</span></a></li></ol></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%85%B6%E5%AE%83%E5%AE%9E%E7%94%A8%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B%E8%BD%AC%E6%8D%A2"><span class="toc-number">4.</span> <span class="toc-text">其它实用数据类型转换</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#Date%E7%B1%BB%E4%B8%8E%E5%85%B6%E5%AE%83%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B%E7%9A%84%E7%9B%B8%E4%BA%92%E8%BD%AC%E6%8D%A2"><span class="toc-number">4.0.1.</span> <span class="toc-text">Date类与其它数据类型的相互转换</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#js%E4%B8%AD%E5%B0%86Data%E8%BD%AC%E6%8D%A2%E6%88%90String%E7%B1%BB%E5%9E%8B"><span class="toc-number">4.0.2.</span> <span class="toc-text">js中将Data转换成String类型</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%AD%97%E7%AC%A6%E5%9E%8B%E8%BD%AC%E6%97%B6%E9%97%B4%E5%9E%8B"><span class="toc-number">4.0.3.</span> <span class="toc-text">字符型转时间型</span></a></li></ol></li></ol></li></ol>
    </nav>
  </div>
</aside>

<main class="main" role="main">
  <div class="content">
  <article id="post-Java类型转换" class="article article-type-post" itemscope itemtype="http://schema.org/BlogPosting">
    
    <div class="article-header">
      
        
  
    <h1 class="article-title" itemprop="name">
      Java类型转换
    </h1>
  

      
      <div class="article-meta">
        <span class="article-date">
    <i class="icon icon-calendar-check"></i>
	<a href="/posts/16b8fe02.html" class="article-date">
	  <time datetime="2021-03-20T07:27:14.000Z" itemprop="datePublished">2021-03-20</time>
	</a>
</span>
        
  <span class="article-category">
    <i class="icon icon-folder"></i>
    <a class="article-category-link" href="/categories/Java/">Java</a>
  </span>

        
  <span class="article-tag">
    <i class="icon icon-tags"></i>
	<a class="article-tag-link-link" href="/tags/Java/" rel="tag">Java</a>, <a class="article-tag-link-link" href="/tags/%E7%B1%BB%E5%9E%8B%E8%BD%AC%E6%8D%A2/" rel="tag">类型转换</a>
  </span>


        
	<span class="article-read hidden-xs">
	    <i class="icon icon-eye-fill" aria-hidden="true"></i>
	    <span id="busuanzi_container_page_pv">
			<span id="busuanzi_value_page_pv">0</span>
		</span>
	</span>


        <span class="post-comment"><i class="icon icon-comment"></i> <a href="/posts/16b8fe02.html#comments" class="article-comment-link">评论</a></span>
        
	
		<span class="post-wordcount hidden-xs" itemprop="wordCount">字数统计: 1.2k(字)</span>
	
	
		<span class="post-readcount hidden-xs" itemprop="timeRequired">阅读时长: 5(分)</span>
	

      </div>
    </div>
    <div class="article-entry marked-body" itemprop="articleBody">
      
        <p><strong>Java一共有八种基本数据类型</strong></p>
<ul>
<li>整数类型<ul>
<li>1字节:byte<ul>
<li>表数范围为-128(-2<sup>7</sup>)~127(2<sup>7</sup>-1)</li>
</ul>
</li>
<li>2字节:short<ul>
<li>-32768(-2<sup>15</sup>)~32767 (2<sup>15</sup>-1)(大约为32万)</li>
</ul>
</li>
<li>4字节:int<ul>
<li>-2147483648(-2<sup>31</sup>)~2147483647(2<sup>31</sup>-1)(大约为21亿)</li>
</ul>
</li>
<li>8字节:long<ul>
<li>-2<sup>63</sup>~2<sup>63</sup>-1</li>
</ul>
</li>
</ul>
</li>
<li>字符类型<ul>
<li>2字节:char</li>
</ul>
</li>
<li>浮点类型<ul>
<li>4字节:float</li>
<li>8字节:double</li>
</ul>
</li>
<li>布尔类型<ul>
<li>boolean</li>
</ul>
</li>
</ul>
<h2 id="自动类型转换"><a href="#自动类型转换" class="headerlink" title="自动类型转换"></a>自动类型转换</h2><p>Java的所有字符型变量可以相互转换,如果系统支持把某种类型的变量直接赋值给另一种基本类型的变量,这种方式称为自动类型转换.当把一个表数范围小的数值或变量直接赋值给另一个表数范围大的变量时,系统可以进行自动类型转换;否则就需要强制转换.</p>
<p>支持自动类型转换的类型为:byte→short→int→long→float→double和char→int→long→float→double</p>
<p><strong><em>字符串和任何基本类型的值进行连接运算时,基本类型的值都会自动转化为字符串类型</em></strong></p>
<h2 id="强制类型转换"><a href="#强制类型转换" class="headerlink" title="强制类型转换"></a>强制类型转换</h2><p>当需要把箭头右边的类型转换为箭头左边的类型时,必须进行强制类型转换强制类型转换的语法格式是:<code>(targetType)value</code>,强制类型转换的运算符是<code>()</code>.转换时要注意数据溢出问题.</p>
<h2 id="字符串类型与其他类型的转换"><a href="#字符串类型与其他类型的转换" class="headerlink" title="字符串类型与其他类型的转换"></a>字符串类型与其他类型的转换</h2><h4 id="其它类型向字符串的转换"><a href="#其它类型向字符串的转换" class="headerlink" title="其它类型向字符串的转换"></a>其它类型向字符串的转换</h4><ol>
<li>调用类的串转换方法:X.toString();</li>
<li>自动转换:X+“”;</li>
<li>使用String的方法:String.valueOf(X);</li>
</ol>
<h4 id="字符串作为值-向其它类型的转换"><a href="#字符串作为值-向其它类型的转换" class="headerlink" title="字符串作为值,向其它类型的转换"></a>字符串作为值,向其它类型的转换</h4><ol>
<li>先转换成相应的封装器实例,再调用对应的方法转换成其它类型</li>
</ol>
<p>例如，字符中“32.1”转换double型的值的格式为:new Float(“32.1”).doubleValue()。</p>
<p>也可以用:Double.valueOf(“32.1”).doubleValue()</p>
<ol start="2">
<li>静态parseXXX方法</li>
</ol>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">String s = <span class="string">&quot;1&quot;</span>;</span><br><span class="line"><span class="keyword">byte</span> b = Byte.parseByte( s );</span><br><span class="line"><span class="keyword">short</span> t = Short.parseShort( s );</span><br><span class="line"><span class="keyword">int</span> i = Integer.parseInt( s );</span><br><span class="line"><span class="keyword">long</span> l = Long.parseLong( s );</span><br><span class="line">Float f = Float.parseFloat( s );</span><br><span class="line">Double d = Double.parseDouble( s );</span><br></pre></td></tr></table></figure>
<ol start="3">
<li>Character的getNumericValue(char ch)方法</li>
</ol>
<p>返回指定的 Unicode 字符表示的 <code>int</code> 值。</p>
<h2 id="其它实用数据类型转换"><a href="#其它实用数据类型转换" class="headerlink" title="其它实用数据类型转换"></a>其它实用数据类型转换</h2><h4 id="Date类与其它数据类型的相互转换"><a href="#Date类与其它数据类型的相互转换" class="headerlink" title="Date类与其它数据类型的相互转换"></a>Date类与其它数据类型的相互转换</h4><p>整型和Date类之间并不存在直接的对应关系，只是你可以使用int型为分别表示年、月、日、时、分、秒，这样就在两者之间建立了一个对应关系，在作这种转换时，你可以使用<strong>Date类构造函数的三种形式</strong>：</p>
<p>①Date(int year, int month, int date)：以int型表示年、月、日</p>
<p>②Date(int year, int month, int date, int hrs, int min)：以int型表示年、月、日、时、分</p>
<p>③Date(int year, int month, int date, int hrs, int min, int sec)：以int型表示年、月、日、时、</p>
<p>分、秒</p>
<p>在长整型和Date类之间有一个很有趣的对应关系，就是将一个时间表示为距离格林尼治标准时间1970年1月1日0时0分0秒的毫秒数。对于这种对应关系，Date类也有其相应的构造函数：Date(long date)。</p>
<p>获取Date类中的年、月、日、时、分、秒以及星期你可以使用Date类的getYear()、getMonth()、getDate()、getHours()、getMinutes()、getSeconds()、getDay()方法，你也可以将其理解为将Date类转换成int。</p>
<p>而Date类的getTime()方法可以得到我们前面所说的一个时间对应的长整型数，与包装类一样，Date类也有一个toString()方法可以将其转换为String类。</p>
<p>有时我们希望得到Date的特定格式，例如20020324，我们可以使用以下方法，首先在文件开始引入，</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> java.text.SimpleDateFormat;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.util.*;</span><br><span class="line"></span><br><span class="line">java.util.Date date = <span class="keyword">new</span> java.util.Date();</span><br><span class="line"></span><br><span class="line"><span class="comment">//如果希望得到YYYYMMDD的格式</span></span><br><span class="line"></span><br><span class="line">SimpleDateFormat sdf=<span class="keyword">new</span> SimpleDateFormat(<span class="string">&quot;yyyyMMDD&quot;</span>);</span><br><span class="line"></span><br><span class="line">String dateFormat=sdf.format(date);</span><br><span class="line"></span><br><span class="line"><span class="comment">//如果希望分开得到年，月，日</span></span><br><span class="line"></span><br><span class="line">SimpleDateFormat sy=newSimpleDateFormat(<span class="string">&quot;yyyy&quot;</span>);</span><br><span class="line"></span><br><span class="line">SimpleDateFormat sm=<span class="keyword">new</span> SimpleDateFormat(<span class="string">&quot;MM&quot;</span>);</span><br><span class="line"></span><br><span class="line">SimpleDateFormat sd=<span class="keyword">new</span> SimpleDateFormat(<span class="string">&quot;dd&quot;</span>);</span><br><span class="line"></span><br><span class="line">String syear=sy.format(date);</span><br><span class="line"></span><br><span class="line">String smon=sm.format(date);</span><br><span class="line"></span><br><span class="line">String sday=sd.format(date);</span><br></pre></td></tr></table></figure>


<h4 id="js中将Data转换成String类型"><a href="#js中将Data转换成String类型" class="headerlink" title="js中将Data转换成String类型"></a>js中将Data转换成String类型</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//对Date的扩展，将 Date转化为指定格式的String</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 月(M)、日(d)、12小时(h)、24小时(H)、分(m)、秒(s)、周(E)、季度(q) 可以用 1-2个占位符</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 年(y)可以用 1-4 个占位符，毫秒(S)只能用 1 个占位符(是 1-3位的数字)</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//eg:</span></span><br><span class="line"></span><br><span class="line">(<span class="keyword">new</span> <span class="built_in">Date</span>()).pattern(<span class="string">&quot;yyyy-MM-dd hh:mm:ss.S&quot;</span>) ==&gt;<span class="number">2006</span>-<span class="number">07</span>-<span class="number">02</span> <span class="number">08</span>:<span class="number">09</span>:<span class="number">04</span><span class="number">.423</span></span><br><span class="line"></span><br><span class="line">(<span class="keyword">new</span> <span class="built_in">Date</span>()).pattern(<span class="string">&quot;yyyy-MM-dd E HH:mm:ss&quot;</span>) ==&gt;<span class="number">2009</span>-<span class="number">03</span>-<span class="number">10</span> 二 <span class="number">20</span>:<span class="number">09</span>:<span class="number">04</span></span><br><span class="line"></span><br><span class="line">(<span class="keyword">new</span> <span class="built_in">Date</span>()).pattern(<span class="string">&quot;yyyy-MM-dd EE hh:mm:ss&quot;</span>) ==&gt;<span class="number">2009</span>-<span class="number">03</span>-<span class="number">10</span> 周二 <span class="number">08</span>:<span class="number">09</span>:<span class="number">04</span></span><br><span class="line"></span><br><span class="line">(<span class="keyword">new</span> <span class="built_in">Date</span>()).pattern(<span class="string">&quot;yyyy-MM-dd EEE hh:mm:ss&quot;</span>)==&gt; <span class="number">2009</span>-<span class="number">03</span>-<span class="number">10</span> 星期二 <span class="number">08</span>:<span class="number">09</span>:<span class="number">04</span></span><br><span class="line"></span><br><span class="line">(<span class="keyword">new</span> <span class="built_in">Date</span>()).pattern(<span class="string">&quot;yyyy-M-d h:m:s.S&quot;</span>) ==&gt;<span class="number">2006</span>-<span class="number">7</span>-<span class="number">2</span> <span class="number">8</span>:<span class="number">9</span>:<span class="number">4.</span></span><br></pre></td></tr></table></figure>


<h4 id="字符型转时间型"><a href="#字符型转时间型" class="headerlink" title="字符型转时间型"></a>字符型转时间型</h4><p>（2005-10-1）</p>
<p>reportdate_str ＝ “2005-10-01”；</p>
<p>reportdate_str ＝ reportdate_str ＋ “ 00:00:00.0”</p>
<p>reportdate = <strong>java.sql.Timestamp.valueOf(reportdate_str);</strong></p>

      
    </div>
    <div class="article-footer">
      <blockquote class="mt-2x">
  <ul class="post-copyright list-unstyled">
    
    <li class="post-copyright-link hidden-xs">
      <strong>本文链接：</strong>
      <a href="https://zzy521.com/posts/16b8fe02.html" title="Java类型转换" target="_blank" rel="external">https://zzy521.com/posts/16b8fe02.html</a>
    </li>
    
    <li class="post-copyright-license">
      <strong>版权声明： </strong> 本博客所有文章除特别声明外，均采用 <a href="http://creativecommons.org/licenses/by/4.0/deed.zh" target="_blank" rel="external">CC BY 4.0 CN协议</a> 许可协议。转载请注明出处！
    </li>
  </ul>
</blockquote>


<div class="panel panel-default panel-badger">
  <div class="panel-body">
    <figure class="media">
      <div class="media-left">
        <a href="https://github.com/zzy2019520" target="_blank" class="img-burn thumb-sm visible-lg">
          <img src="/images/avatar.jpg" class="img-rounded w-full" alt="">
        </a>
      </div>
      <div class="media-body">
        <h3 class="media-heading"><a href="https://github.com/zzy2019520" target="_blank"><span class="text-dark">张占阳</span><small class="ml-1x">软件开发工程师</small></a></h3>
        <div>能吃能睡，啥都不会！</div>
      </div>
    </figure>
  </div>
</div>


    </div>
  </article>
  
    
  <section id="comments">
  	
      <div id="vcomments"></div>
    
  </section>


  
</div>

  <nav class="bar bar-footer clearfix" data-stick-bottom>
  <div class="bar-inner">
  
  <ul class="pager pull-left">
    
    <li class="prev">
      <a href="/posts/f789481e.html" title="Java位运算"><i class="icon icon-angle-left" aria-hidden="true"></i><span>&nbsp;&nbsp;上一篇</span></a>
    </li>
    
    
    <li class="next">
      <a href="/posts/6c0fc8c8.html" title="Python处理Excel文件"><span>下一篇&nbsp;&nbsp;</span><i class="icon icon-angle-right" aria-hidden="true"></i></a>
    </li>
    
    
    <li class="toggle-toc">
      <a class="toggle-btn collapsed" data-toggle="collapse" href="#collapseToc" aria-expanded="false" title="文章目录" role="button">
        <span>[&nbsp;</span><span>文章目录</span>
        <i class="text-collapsed icon icon-anchor"></i>
        <i class="text-in icon icon-close"></i>
        <span>]</span>
      </a>
    </li>
    
  </ul>
  
  
  <!-- Button trigger modal -->
  <button type="button" class="btn btn-fancy btn-donate pop-onhover bg-gradient-warning" data-toggle="modal" data-target="#donateModal"><span>赏</span></button>
  <!-- <div class="wave-icon wave-icon-danger btn-donate" data-toggle="modal" data-target="#donateModal">
    <div class="wave-circle"><span class="icon"><i class="icon icon-bill"></i></span></div>
  </div> -->
  
  
  <div class="bar-right">
    
  </div>
  </div>
</nav>
  
<!-- Modal -->
<div class="modal modal-center modal-small modal-xs-full fade" id="donateModal" tabindex="-1" role="dialog">
  <div class="modal-dialog" role="document">
    <div class="modal-content donate">
      <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
      <div class="modal-body">
        <div class="donate-box">
          <div class="donate-head">
            <p>感谢您的支持，我会继续努力的!</p>
          </div>
          <div class="tab-content">
            <div role="tabpanel" class="tab-pane fade active in" id="alipay">
              <div class="donate-payimg">
                <img src="/images/donate/alipay.jpg" alt="扫码支持" title="扫一扫" />
              </div>
              <p class="text-muted mv">扫码打赏，你说多少就多少</p>
              <p class="text-grey">打开支付宝扫一扫，即可进行扫码打赏哦</p>
            </div>
            <div role="tabpanel" class="tab-pane fade" id="wechatpay">
              <div class="donate-payimg">
                <img src="/images/donate/wechat.png" alt="扫码支持" title="扫一扫" />
              </div>
              <p class="text-muted mv">扫码打赏，你说多少就多少</p>
              <p class="text-grey">打开微信扫一扫，即可进行扫码打赏哦</p>
            </div>
          </div>
          <div class="donate-footer">
            <ul class="nav nav-tabs nav-justified" role="tablist">
              <li role="presentation" class="active">
                <a href="#alipay" id="alipay-tab" role="tab" data-toggle="tab" aria-controls="alipay" aria-expanded="true"><i class="icon icon-alipay"></i> 支付宝</a>
              </li>
              <li role="presentation" class="">
                <a href="#wechatpay" role="tab" id="wechatpay-tab" data-toggle="tab" aria-controls="wechatpay" aria-expanded="false"><i class="icon icon-wepay"></i> 微信支付</a>
              </li>
            </ul>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>



</main>

  <footer class="footer" itemscope itemtype="http://schema.org/WPFooter">
	
	
    <ul class="social-links">
    	
        <li><a href="https://github.com/zzy2019520" target="_blank" title="Github" data-toggle=tooltip data-placement=top><i class="icon icon-github"></i></a></li>
        
        <li><a href="https://weibo.com/u/6000361757" target="_blank" title="Weibo" data-toggle=tooltip data-placement=top><i class="icon icon-weibo"></i></a></li>
        
    </ul>

    <div class="copyright">
    	
        &copy; 2021 张占阳
        
        <div class="publishby">
            本网站由<a href="https://www.upyun.com/?utm_source=lianmeng&utm_medium=referral" target="_blank"><img width="40" src="../img/又拍云_logo5.png"/> </a>提供存储服务
        	<!-- Theme by <a href="https://github.com/cofess" target="_blank"> cofess </a>base on <a href="https://github.com/cofess/hexo-theme-pure" target="_blank">pure</a>. -->
        </div>
    </div>
</footer>
  <script src="//cdn.jsdelivr.net/npm/jquery@1.12.4/dist/jquery.min.js"></script>
<script>
window.jQuery || document.write('<script src="js/jquery.min.js"><\/script>')
</script>

<script src="/js/plugin.min.js"></script>


<script src="/js/application.js"></script>


    <script>
(function (window) {
    var INSIGHT_CONFIG = {
        TRANSLATION: {
            POSTS: '文章',
            PAGES: '页面',
            CATEGORIES: '分类',
            TAGS: '标签',
            UNTITLED: '(未命名)',
        },
        ROOT_URL: '/',
        CONTENT_URL: '/content.json',
    };
    window.INSIGHT_CONFIG = INSIGHT_CONFIG;
})(window);
</script>

<script src="/js/insight.js"></script>






   
<script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>





   
    
  <script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
  <script src="//cdn.jsdelivr.net/npm/valine"></script>
  <script type="text/javascript">
  var GUEST = ['nick', 'mail', 'link'];
  var meta = 'nick,mail,link';
  meta = meta.split(',').filter(function(item) {
    return GUEST.indexOf(item) > -1;
  });
  new Valine({
    el: '#vcomments',
    verify: false,
    notify: false,
    appId: 'SVT1dStIdP36PNVntBfpuIAa-gzGzoHsz',
    appKey: 'wXS1sNhJ0U7pxRdcjaxUCJIk',
    placeholder: '填写评论',
    avatar: 'mm',
    meta: meta,
    pageSize: '10' || 10,
    visitor: false
  });
  </script>

     







</body>
</html>